package com.tim.app.ui.activity.person_center;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.application.library.runtime.event.EventManager;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.server.api.API_User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.InputInvateCodeBean;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.activity.BaseActivity;
import com.tim.app.util.MyYAnimation;
import com.tim.app.util.TextUtil;
import com.tim.app.util.ToastUtil;

/**
 * Created by wdy on 2017/8/21.
 */

public class InputInvateCodeActivity extends BaseActivity {

    private ImageButton ibBack;
    private ImageView iv_redbag_open;
    private EditText et_invatecode;
    private MyYAnimation myYAnimation;
    private TextView tv_receive_redbag, tv_wx_get, tv_money_monent;
    private ImageView iv_redbag_top, iv_redbag_bottom;
    private TextView btn_commit;

    public static void start(Context context) {
        Intent intent = new Intent(context, InputInvateCodeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        return R.layout.activity_inputinvatecode;
    }

    @Override
    public void initView() {
        tv_money_monent = (TextView) findViewById(R.id.tv_money_monent);
        tv_wx_get = (TextView) findViewById(R.id.tv_wx_get);
        ibBack = (ImageButton) findViewById(R.id.ibBack);
        iv_redbag_open = (ImageView) findViewById(R.id.iv_redbag_open);
        et_invatecode = (EditText) findViewById(R.id.et_invatecode);
        btn_commit = (TextView) findViewById(R.id.btn_commit);
        iv_redbag_bottom = (ImageView) findViewById(R.id.iv_redbag_bottom);
        iv_redbag_top = (ImageView) findViewById(R.id.iv_redbag_top);
        tv_receive_redbag = (TextView) findViewById(R.id.tv_receive_redbag);
        iv_redbag_open.setOnClickListener(this);
        ibBack.setOnClickListener(this);
        btn_commit.setOnClickListener(this);
    }

    @Override
    public void initData() {
        myYAnimation = new MyYAnimation();
        myYAnimation.setRepeatCount(1);
        myYAnimation.setAnimationListener(animationListener);
    }

    private Animation.AnimationListener animationListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            tv_receive_redbag.setVisibility(View.GONE);
            et_invatecode.setVisibility(View.GONE);
            tv_wx_get.setVisibility(View.GONE);
            Animation animation1 = AnimationUtils.loadAnimation(InputInvateCodeActivity.this, R.anim.red_top_out);
            animation1.setFillAfter(true);
            iv_redbag_top.setAnimation(animation1);
            animation1.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    iv_redbag_open.setBackgroundResource(R.drawable.iv_money);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            //开
            Animation openAni = AnimationUtils.loadAnimation(InputInvateCodeActivity.this, R.anim.open_top_out);
            openAni.setFillAfter(true);
            iv_redbag_open.setAnimation(openAni);

            //底部背景
            Animation bottomAni = AnimationUtils.loadAnimation(InputInvateCodeActivity.this, R.anim.red_bottom_out);
            bottomAni.setFillAfter(true);
            iv_redbag_bottom.setAnimation(bottomAni);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };


    @Override
    public void onClick(View v) {
        if (v == ibBack) {
            finish();
        } else if (v == iv_redbag_open) {
            if (TextUtils.isEmpty(et_invatecode.getText().toString().trim())) {
                ToastUtil.showToast("请输入邀请码");
            } else {
                setInputinvatecode();
                //    iv_redbag_open.startAnimation(myYAnimation);
            }
        } else if (v == btn_commit) {
            finish();
        }
    }

    private void setInputinvatecode() {
        API_User.ins().setInputinvatecode(TAG, UserManager.ins().getUserId(), et_invatecode.getText().toString().trim(), new StringResponseCallback() {
            @Override
            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                //      Log.e("tag", "resultValue====" + resultValue);
                InputInvateCodeBean inputInvateCodeBean = new Gson().fromJson(resultValue, InputInvateCodeBean.class);
                if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                    tv_money_monent.setText("￥" + inputInvateCodeBean.getRes().getPropertyList().get(0).getPropertyNum());
                    iv_redbag_open.startAnimation(myYAnimation);
                    UserManager.ins().getLoginUser().setInviteUuid("111");
                    //   EventManager.ins().sendEvent(EventTag.INPUTINVATECODE, 0, 0, "");
                } else if (errCode == 130200) {
                    ToastUtil.showToast("邀请码错误");
                } else {
                    ToastUtil.showToast(inputInvateCodeBean.getMessage());
                }
                return false;
            }
        });
    }
}
